import Vue from 'vue'
import baseURL from '@/utils/request/baseURL'

const token = uni.getStorageSync('token')
const vertoken = uni.getStorageSync('vertoken')
const userInfo = uni.getStorageSync('userInfo')
const state = {
	baseURL: baseURL,
	token: token,
	vertoken:vertoken,
	userInfo: { ...userInfo },
	selAddress: {},
	update: false,
	loadingMore: false,
	isLoading: false,
	isBlock: {},
	commityIng:{},
	downloadURL: { 'Android': '', 'iOS': '' },
	isShowEye: true
}

const mutations = {
	SET_STATE: (state, { data, attr }) => {
		state[attr] = data
	},
	SET_ADDRESS: (state, data) => {
		state.selAddress = data
	},
	SET_UPDATE: (state, data) => {
		state.update = data
	},
	SET_COMMITY: (state, data) => {
		state.commityIng = data
		console.log(state.commityIng)
	},
	SET_LOADINGMORE: (state, data) => {
		state.loadingMore = data
	},
	SET_ISLOADING: (state, data) => {
		state.isLoading = data
	},
	SET_ISBLOCK: (state, data) => {
		state.isBlock = data
	},
	SET_TOKEN: (state, data) => {
		state.token = data
	},
	SET_VERTOKEN: (state, data) => {
		state.vertoken = data
	},
	SET_USER_INFO: (state, data) => {
		state.userInfo = { ...state.userInfo, ...data }
	},
	UPDATE_USER: ({ userInfo }, { attr, value }) => {
		userInfo[attr] = value
	},
	SET_SHOWEYE: (state, data) => {
		state.isShowEye = data
	},
}

const actions = {
	setState({ commit }, data) {
		commit('SET_STATE', data)
	},
	setCommity({ commit }, data) {
		commit('SET_COMMITY', data)
	},
	setAddress({ commit }, data) {
		commit('SET_ADDRESS', data)
	},
	setUpdate({ commit }, data) {
		commit('SET_UPDATE', data)
	},
	setToken({ commit }, data) {
		commit('SET_TOKEN', data)
		uni.setStorageSync('token', data)
	},
	setVertoken({ commit }, data) {
		commit('SET_VERTOKEN', data)
		uni.setStorageSync('vertoken', data)
	},
	setUserInfo({ commit }, data) {
		commit('SET_USER_INFO', data)
		uni.setStorageSync('userInfo', data)
	},
	async getUser({ commit }) {
		let { data } = await Vue.http.post('user.getUserInfo', {}, { tipConfig: { isLoading: false } })
		commit('SET_USER_INFO', data)
		uni.setStorageSync('userInfo', data)
	},
	async logout({ commit }) {
		commit('SET_USER_INFO', '')
		commit('SET_TOKEN', '')
		uni.setStorageSync('userInfo', '')
		uni.setStorageSync('token', '')
	},
	updateUser({ commit }, data) {
		commit('UPDATE_USER', data)
	},
	setShowEye({ commit }, data) {
		commit('SET_SHOWEYE', data)
	},
}

export default { state, mutations, actions }
